package com.qbw.app.module.launch;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.gosuncnwelink.twowheels.R;
import com.gosuncnwelink.twowheels.databinding.LaunchActivityBinding;
import com.http.okhttp.HttpTask;
import com.qbw.app.manager.UserManager;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.main.activity.MainActivity;
import com.qbw.app.module.user.activity.UserLoginRegistActivity;
import com.qbw.app.module.vehicle.activity.VehicleBindByScanActivity;
import com.qbw.app.request.impl.VehicleRequest;

/*
 * @author QBW
 * @date 2019/8/13
 */
public class LaunchActivity extends CommonActivity<LaunchActivityBinding>{

    private Handler mHandler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.launch_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        showLoadingDialog("");
        mHandler.postDelayed(() -> {
            checkWhereToTo();
        }, 1500);
        //autoLogin();
    }

    /*private void autoLogin() {
     *//*app的账号：{
            "telCode":"+86",
                    "phone":"17777777777",
                    "password":"123456"
        }*//*
        showLoadingDialog(R.string.loading_please_wait);
        mGeneralManager.getRequestManager().getUserRequest().login("+86", "17777777777",
                                                                   "123456", this).execute();
    }*/

    private void checkWhereToTo() {
        if (UserManager.getInstance().isLogin()) {
            // 检查车辆
            mGeneralManager.getRequestManager().getVehicleRequest().getVehicleList(this).execute();
        } else {
            closeLoadingDialog();
            UserLoginRegistActivity.start(this);
            finish();
        }
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onHttpStart(HttpTask httpTask) {
        super.onHttpStart(httpTask);
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        closeLoadingDialog();
        String method = httpTask.getMethod();
        /*if (UserRequest.METHOD_LOGIN.equals(method)) {
            MainActivity.start(LaunchActivity.this);
        } else */
        if (method.equals(VehicleRequest.METHOD_VEHICLE_LIST)) {
            Vehicle.Response res = (Vehicle.Response) entity;
            if (res.getData().isEmpty()) {
                //没有车辆
                VehicleBindByScanActivity.startActivity(this, VehicleBindByScanActivity.TYPE_FROM_NO_VEHICLE);
            } else {
                MainActivity.start(this);
            }
            finishAffinity();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        closeLoadingDialog();
        UniversalToastUtil.showFailedIconToast(message);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        LanguageUtils.setdefaultLanguage(LaunchActivity.this, SPUtils.getInstance().getString("language"));

    }
}
